package com.flexycore.iphone.controller;

import java.util.Vector;


import com.flexycore.iphone.coregraphics.CGRect;
import com.flexycore.iphone.uikit.NSIndexPath;
import com.flexycore.iphone.uikit.UIBarButtonItem;
import com.flexycore.iphone.uikit.UIBarButtonItemStyle;
import com.flexycore.iphone.uikit.UITableView;
import com.flexycore.iphone.uikit.UITableViewCell;
import com.flexycore.iphone.uikit.UITableViewCellEditingStyle;
import com.flexycore.iphone.uikit.UITableViewController;
import com.flexycore.iphone.uikit.UITableViewStyle;
import com.flexycore.iphone.uikit.UIViewController;
import com.flexycore.iphone.view.EditCellTextView;

/*********************************************************************************
 * 
 * @author matthieu nicolazo
 * 
 * Manage a TableView.
 * This TableView can be edited by adding rows.
 *
 */
public class AddCellsController extends UITableViewController {
	/*********************************************************************************
	 * Field
	 */
	private Vector<String> data;
	
	
	/*********************************************************************************
	 * Constructor
	 */
	public AddCellsController(){
		super();
		initWithStyle(UITableViewStyle.UITableViewStylePlain);
		setTitle("Spices");
		
		data = new Vector<String>();
		data.add("Salt");
		data.add("Pepper");
		data.add("Curry");
		
		// Put an "edit" button on the right side of navigation bar.
		this.navigationItem().setRightBarButtonItemAnimated(this.editButtonItem(), true);
	}
	
	
	/*********************************************************************************
	 * 
	 * @param	table		UITableView calling this method.
	 */
	@Override
	public int numberOfSectionsInTableView(UITableView table){
		return 1;
	}

	/*********************************************************************************
	 * 
	 * @param	table		UITableView calling this method.
	 * @param	section		Section in which rows will be displayed.
	 * @return				The number of rows in section equals to data size + one cell
	 * 						for adding a new element.
	 */
	@Override
	public int tableViewNumberOfRowsInSection(UITableView table, int section){
		return data.size()+1;
	}
	
	/*********************************************************************************
	 * 
	 * @return UITableViewCell	cell displaying in index section and row.
	 */
	@Override
	public UITableViewCell tableViewCellForRowAtIndexPath(UITableView t, NSIndexPath indexPath){
		UITableViewCell cell = new UITableViewCell();
		cell.initWithFrame(CGRect.CGRectZero());
		if(indexPath.row() < data.size()){
			String txt = data.get(indexPath.row());
			cell.setText(txt);
		}
		return cell;
	}
	
	/*********************************************************************************
	 * 
	 * @param	tableView		UITableView calling this method.
	 * @param	indexPath		Section and row of cell.
	 * @return					UITableViewCellEditingStyle for this cell.
	 */
	@Override
	public int tableViewEditingStyleForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath) {
		if(indexPath.row() < data.size())
			return UITableViewCellEditingStyle.UITableViewCellEditingStyleNone;
		else
			return UITableViewCellEditingStyle.UITableViewCellEditingStyleInsert;
	}
	
	/*********************************************************************************
	 * 
	 * @param	tableView		UITableView calling this method.
	 * @param	editingStyle	Editing style of cell at indexPath.
	 * @param	indexPath		Section and row of cell.
	 */
	@Override
	public void tableViewCommitEditingStyleForRowAtIndexPath( UITableView tableView, int editingStyle, 
			NSIndexPath indexPath) {
		this.tableView().beginUpdates();
		
		// Add cell at indexPath
		if(editingStyle == UITableViewCellEditingStyle.UITableViewCellEditingStyleInsert){
			// Before, launch a new UIView to complete the cell text
			launchEditCellTextView();
		}
		
		this.tableView().endUpdates();
	}
	
	/*********************************************************************************
	 * Create a new View to allow user enter specific text in the new cell.
	 * Push this view in the NavigatioController stack.
	 */
	public void launchEditCellTextView() {
		UIViewController viewCtr = new UIViewController();
		viewCtr.init();
		viewCtr.setTitle("Edit Text");
		EditCellTextView view = new EditCellTextView();
		viewCtr.setView(view);
		
		SaveBarButton save = new SaveBarButton(this, view);
		viewCtr.navigationItem().setRightBarButtonItemAnimated(save, true);
		
		this.navigationController().pushViewControllerAnimated(viewCtr, true);
	}
	
	
	/*********************************************************************************
	 * 
	 * @author matthieu nicolazo
	 *
	 */
	public class SaveBarButton extends UIBarButtonItem {
		private AddCellsController _ctr;
		private EditCellTextView _view;
		public SaveBarButton(AddCellsController ctr, EditCellTextView view){
			super();
			initWithTitleStyleTargetAction("save", UIBarButtonItemStyle.UIBarButtonItemStylePlain);
			_ctr = ctr;
			_view = view;
		}
		@Override
		public void buttonClicked() {
			String text = _view.getText();
			_ctr.data.add(text);
			_ctr.navigationController().popViewControllerAnimated(true);
			_ctr.tableView().reloadData();
		}
	}
	
}
